﻿using Microsoft.EntityFrameworkCore;
using TuLing.Dto;
using TuLing.Entities;
using TuLing.IServices;

namespace TuLing.Services
{
    public class SupplierService : BaseService, ISupplierService
    {
        public SupplierService(DefaultDbContext context) : base(context) { }

        public async Task<List<SupplierDto>> Search(string keyword="")
        {
            var list = await _context.Suppliers.ToListAsync();
            
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                list = list.Where(m => m.Name.Contains(keyword)).ToList();
            }

            return list.Select(m => new SupplierDto
            {
                Id = m.Id,
                Name = m.Name,
                Phone = m.Phone,
                Address = m.Address,
                Balance = m.Balance,
                Remark = m.Remark
            }).ToList();
        }

        public async Task Save(SupplierDto input)
        {
            var has = await _context.Suppliers.AnyAsync(m => m.Name.Equals(input.Name) && m.Id != input.Id);
            if (has)
            {
              throw new Exception("名称已存在，请检查");
            }

            if (input.Id > 0)
            {

                var first = await _context.Suppliers.FirstOrDefaultAsync(m => m.Id == input.Id);
                if (first == null)
                {
                    throw new Exception("供应商不存在");
                }

                // 更新供应商信息
                first.Name = input.Name;
                first.Phone = input.Phone;
                first.Address = input.Address;
                first.Balance = input.Balance;
                first.Remark = input.Remark;

                // 更新供应商信息
                _context.Suppliers.Update(first);
                await _context.SaveChangesAsync();

            }
            else
            {
                var s = new Supplier
                {
                    Name = input.Name,
                    Phone = input.Phone,
                    Address = input.Address,
                    Balance = input.Balance,
                    Remark = input.Remark
                };
                _context.Suppliers.Add(s);
                await _context.SaveChangesAsync();
                
                // 更新成功后，返回更新的ID
                input.Id = s.Id;
            }
        }

        /// <summary>
        /// 获取 
        /// </summary>  
        public async Task<SupplierDto?> Get(long id)
        {
            var first = await _context.Suppliers.FirstOrDefaultAsync(s => s.Id == id);
            if (first == null)
            {
                return null;
            }
            return new SupplierDto
            {
                Id = first.Id,
                Name = first.Name,
                Phone = first.Phone,
                Address = first.Address,
                Balance = first.Balance,
                Remark = first.Remark
            };
        }

        /// <summary>
        /// 删除 
        /// </summary>  
        public async Task Delete(long id)
        {
            var s = await _context.Suppliers.FirstOrDefaultAsync(m => m.Id == id);
            if (s == null)
            {
                throw new Exception("供应商不存在");
            }
            _context.Suppliers.Remove(s);
            await _context.SaveChangesAsync();
        }
      
    }
}
